/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PurchaseOrder;
import java.sql.*;

/**
 *
 * @author sundas
 */
public class PDB {
    private Connection con;
    /*private Connection con = null;
    public void connect(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:mysql:newpurchase","root","yarehman");
System.out.println("Connected to the database");
con.close();
System.out.println("Disconnected from database");
}
catch (Exception e) {
e.printStackTrace();
}
    }*/

    public void connect(){
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            //Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/newpur", "root", "root");

            if (!con.isClosed()) {
                System.out.println("Successfully connected to " +
                        "MySQL server using TCP/IP...");
            }

        } catch (Exception e) {
            System.err.println("connect() Exception: " + e.getMessage());
        }
    }
    public void FillOurCompanyName(javax.swing.JComboBox ourcompany){
        try {
            Statement st = con.createStatement();
            st.execute("select inventory.inventoryName from inventory");
            ResultSet rs =  st.getResultSet();

            while(rs.next()){
                String name = rs.getString("InventoryName");

                ourcompany.addItem(name);
            }

        } catch (SQLException e) {
            System.err.println("FillOurCompanyName() Exception: " + e.getMessage());
        }
    }
    public void FillSalePersonBox(javax.swing.JComboBox jComboBox1){
        try {
            Statement st = con.createStatement();
            st.execute("select person.name from person inner join supplier where person.NIC= supplier.NIC ");
            ResultSet rs =  st.getResultSet();

            while(rs.next()){
                String name = rs.getString("name");

                jComboBox1.addItem(name);
            }

        } catch (SQLException e) {
            System.err.println("FillSalePersonBox() Exception: " + e.getMessage());
        }
    }

    public void FillCurrentDateField(javax.swing.JTextField PODate){
        try {
            Statement st = con.createStatement();
            st.execute("SELECT CURDATE() ");
            ResultSet rs =  st.getResultSet();
            String date = "";
            while(rs.next())
            {
                date = rs.getString(1);
                //System.out.println(date);
               // st.execute("SELECT DATE_FORMAT('2010-2-1', '%b %c %Y')");
               // rs = st.getResultSet();
               //while(rs.next())
               //{

               // date = rs.getString(1);
                PODate.setText(date);

               // }

            }


        } catch (SQLException e) {
            System.err.println("FillCurrentDateField() Exception: " + e.getMessage());
        }
    }
    public void FillPDescripton(javax.swing.JTextField PDescripton){
        try {
            Statement st = con.createStatement();
            st.execute("select product.description from product");
            ResultSet rs =  st.getResultSet();

            while(rs.next()){
                String description = rs.getString("description");

                PDescripton.setText(description);
            }

        } catch (SQLException e) {
            System.err.println("FillPDescripton() Exception: " + e.getMessage());
        }
    }
 public void FillCurrentDateReive(javax.swing.JTextField DateRecive){
        try {
            Statement st = con.createStatement();
            st.execute("SELECT CURDATE() ");
            ResultSet rs =  st.getResultSet();
            String date = "";
            while(rs.next())
            {
                date = rs.getString(1);
                //System.out.println(date);
               // st.execute("SELECT DATE_FORMAT('2010-2-1', '%b %c %Y')");
               // rs = st.getResultSet();
               //while(rs.next())
               //{

               // date = rs.getString(1);
                DateRecive.setText(date);

               // }

            }


        } catch (SQLException e) {
            System.err.println("FillCurrentDateField() Exception: " + e.getMessage());
        }
    }
 public void FillDepartment(javax.swing.JComboBox  Department){
        try {
            Statement st = con.createStatement();
            st.execute("select department.deptName from department");
            ResultSet rs =  st.getResultSet();

            while(rs.next()){
                String departmentinput = rs.getString("deptName");

                Department.addItem(departmentinput);
            }

        } catch (SQLException e) {
            System.err.println("FillDepartment() Exception: " + e.getMessage());
        }
    }
public void FillGroup(javax.swing.JComboBox Group){
        try {
            Statement st = con.createStatement();
            st.execute("select prodgroup.groupName from prodgroup");
            ResultSet rs =  st.getResultSet();

            while(rs.next()){
                String groupinput = rs.getString("groupName");

                Group.addItem(groupinput);
            }

        } catch (SQLException e) {
            System.err.println("FillGroup() Exception: " + e.getMessage());
        }
    }
public void FillID(javax.swing.JTextField PrOrderNo)
{
    try {
            Statement st = con.createStatement();
            st.execute("select prodorder.orderId from prodorder");
             ResultSet rs =  st.getResultSet();

            while(rs.next()){
                String IDinput = rs.getString("orderId");

                PrOrderNo.setText(IDinput);
            }

        } catch (SQLException e) {
            System.err.println("FillGroup() Exception: " + e.getMessage());
        }
    }
public void FiLLStockno(javax.swing.JTextPane Stocklistno)
{
    try {
            Statement st = con.createStatement();
            st.execute("select stock.stockId from stock");
             ResultSet rs =  st.getResultSet();

            while(rs.next()){
                String stockno = rs.getString("stockId");

                Stocklistno.setText(stockno);
            }

        } catch (SQLException e) {
            System.err.println("FillStockno() Exception: " + e.getMessage());
        }
    }
public void FillQTY(int Qtyv)
{
    try {
        
           // String QTy=SuppliedQTY.getText();
            //System.out.println(QTy);
            
            //int Qtyv=Integer.parseInt(QTy);

            //int aInt = Integer.parseInt(aString);
            Statement st = con.createStatement();
            int id=1;
             st.executeUpdate("update Prodorder set itemCount ='"+Qtyv+"' where orderId ='"+id+"'");
             
        } catch (SQLException e) {
            System.err.println("FillQTY() Exception: " + e.getMessage());
        }
}
public void FillCost(int Cost)
{
    try {

        
            Statement st = con.createStatement();
            int id=1;
             st.executeUpdate("update Product set unitPurchasePrice='"+Cost+"' where productNumber ='"+id+"'");

        } catch (SQLException e) {
            System.err.println("FillCost() Exception: " + e.getMessage());
        }
}
}


